(ns Client.windowClient
  "The Graphical User Interface of the Zenkai Project"
  (:gen-class)
  (:import (javax.swing JFrame JPanel JScrollPane JTable JButton JMenuBar
            JMenu JMenuItem JComboBox JTextField
            JLabel table.AbstractTableModel)
           (java.awt Dimension GridLayout BorderLayout)
           (java.awt.event ActionListener)
  )
  (:use ;Engine.readbinfile
        Client.client
        clojure.contrib.gen-html-docs
        
        
  )
)

;-------------------------------------------------------------------------------
;main frame
(def main-frame (JFrame. "Zenkai Client"))

;create frame
(def create-frame (JFrame. "Create"))

;create panel
(def create-panel (JPanel. ))

;edit frame
(def edit-frame (JFrame. "Edit"))

;edit panel
(def edit-panel (JPanel. ))

;book frame
(def book-frame (JFrame. "Book"))

;book panel
(def book-panel (JPanel. ))

;search panel
(def search-panel (JPanel. ))

;button panel
(def button-panel (JPanel. ))

;table panel
(def table-panel (JPanel. ))

;-------------------------------------------------------------------------------
;elements of the menu bar
(def menu-bar (JMenuBar.))
(def file-menu (JMenu. "File"))
(def edit-menu (JMenu. "Edit"))
(def help-menu (JMenu. "Help"))
(def exit-item (JMenuItem. "Exit"))
(def create-item (JMenuItem. "Create"))
(def edit-item (JMenuItem. "Edit"))
(def delete-item (JMenuItem. "Delete"))
(def book-item (JMenuItem. "Book"))
(def help-item (JMenuItem. "Zenkai Help"))
(def about-item (JMenuItem. "About..."))

;-------------------------------------------------------------------------------
;elements of the button panel
(def create-button (JButton. "Create"))
(def edit-button (JButton. "Edit"))
(def delete-button (JButton. "Delete"))
(def book-button (JButton. "Book"))

;-------------------------------------------------------------------------------
;elements of the search panel
(def search-text (JTextField. 20) )
(def list-combo ["Hotel Name" "City" "Maximum ocuppancy" "Smoking" 
                  "Price" "Date Avalible" "Customer"])
(def combo-box (JComboBox. (into-array list-combo)))
(def search-button (JButton. "Search"))

;-------------------------------------------------------------------------------
;elements of the create frame
;labels
(def hotel-name-label  (JLabel. "Hotel Name:       " JLabel/LEFT))
(def city-label        (JLabel. "City:             " JLabel/LEFT))
(def size-label        (JLabel. "Maximum occupancy:" JLabel/LEFT))
(def smoking-label     (JLabel. "Smoking:          " JLabel/LEFT))
(def price-night-label (JLabel. "Price:            " JLabel/LEFT))
(def date-label        (JLabel. "Date Avalible:    " JLabel/LEFT))
(def owner-label       (JLabel. "Customer:         " JLabel/LEFT))
;text fields
(def hotel-name-text (JTextField. 10))
(def city-text (JTextField. 10))
(def size-text (JTextField. 10))
(def smoking-text (JTextField. 10))
(def price-night-text (JTextField. 10))
(def date-text (JTextField. 10))
(def owner-text (JTextField. 10))
;buttons
(def create-button-cf (JButton. "Create"))
(def cancel-button-cf (JButton. "Cancel"))

;-------------------------------------------------------------------------------
;elements of the edit frame
;labels
(def hotel-name-label-ef  (JLabel. "Hotel Name:       " JLabel/LEFT))
(def city-label-ef        (JLabel. "City:             " JLabel/LEFT))
(def size-label-ef        (JLabel. "Maximum occupancy:" JLabel/LEFT))
(def smoking-label-ef     (JLabel. "Smoking:          " JLabel/LEFT))
(def price-night-label-ef (JLabel. "Price:            " JLabel/LEFT))
(def date-label-ef        (JLabel. "Date Avalible:    " JLabel/LEFT))
(def owner-label-ef       (JLabel. "Customer:         " JLabel/LEFT))
;text fields
(def hotel-name-text-ef (JTextField. 10))
(def city-text-ef (JTextField. 10))
(def size-text-ef (JTextField. 10))
(def smoking-text-ef (JTextField. 10))
(def price-night-text-ef (JTextField. 10))
(def date-text-ef (JTextField. 10))
(def owner-text-ef (JTextField. 10))
;buttons
(def edit-button-ef (JButton. "Edit"))
(def cancel-button-ef (JButton. "Cancel"))

;-------------------------------------------------------------------------------
;elements of the book frame
;labels
(def id-new-owner-label (JLabel. "Owner ID:     " JLabel/LEFT))
;text fields
(def id-new-owner-text (JTextField. 10))
;buttons
(def book-button-bf (JButton. "Book"))
(def cancel-button-bf (JButton. "Cancel"))

;-------------------------------------------------------------------------------
;main table
(def table (JTable.))
(def column-names ["Hotel Name" "City" "Maximum ocuppancy" "Smoking" 
                  "Price" "Date Avalible" "Customer"])
;-------------------------------------------------------------------------------
(defn table-model
  "Implements a Table Model with an AbstractTableModel for the table"
  [data column-names]
  (proxy [AbstractTableModel][]
    (getRowCount [] (count data))
    (getColumnCount [] (count column-names))
    (getValueAt [row column] (nth (nth data row) column))
    (getColumnName [col-num] (nth column-names col-num))
    (isCellEditable [row column] false)
  ))

;-------------------------------------------------------------------------------
(defn create-table
  "Init the table with default data"
  []
  (let [
     default-data (get-records)
     scroll-pane (JScrollPane. table)
     content-pane (.getContentPane main-frame)
    ]
    (.setModel table (table-model (to-array-2d default-data) 
      (into-array column-names)))
    (.setPreferredScrollableViewportSize table (Dimension. 700 200))
    (.add main-frame BorderLayout/NORTH scroll-pane)
  )
)
;-------------------------------------------------------------------------------
(defn create-menu-bar
  "Init the elements of the menu bar"
  []
  (let []
    (.add file-menu exit-item)
    
    (.add edit-menu create-item)
    (.add edit-menu edit-item)
    (.add edit-menu delete-item)
    (.add edit-menu book-item)
    
    (.add help-menu help-item)
    (.add help-menu about-item)
    
    (.add menu-bar file-menu)
    (.add menu-bar edit-menu)
    (.add menu-bar help-menu)
    
    (.setJMenuBar main-frame menu-bar)))
;-------------------------------------------------------------------------------
(defn create-window
  "Build the Create Register Window"
  []
  (let []
    (.add create-panel BorderLayout/CENTER hotel-name-label)
    (.add create-panel BorderLayout/CENTER hotel-name-text)
    (.add create-panel BorderLayout/CENTER city-label)
    (.add create-panel BorderLayout/CENTER city-text)
    (.add create-panel BorderLayout/CENTER size-label)
    (.add create-panel BorderLayout/CENTER size-text)
    (.add create-panel BorderLayout/CENTER smoking-label)
    (.add create-panel BorderLayout/CENTER smoking-text)
    (.add create-panel BorderLayout/CENTER price-night-label)
    (.add create-panel BorderLayout/CENTER price-night-text)
    (.add create-panel BorderLayout/CENTER date-label)
    (.add create-panel BorderLayout/CENTER date-text)
    (.add create-panel BorderLayout/CENTER owner-label)
    (.add create-panel BorderLayout/CENTER owner-text)
    (.add create-panel BorderLayout/SOUTH create-button-cf)
    (.add create-panel BorderLayout/SOUTH cancel-button-cf)
    ))
;-------------------------------------------------------------------------------
(defn edit-window
  "Build the Edit Register Window"
  []
  (let []
    (.add edit-panel BorderLayout/CENTER hotel-name-label-ef)
    (.add edit-panel BorderLayout/CENTER hotel-name-text-ef)
    (.add edit-panel BorderLayout/CENTER city-label-ef)
    (.add edit-panel BorderLayout/CENTER city-text-ef)
    (.add edit-panel BorderLayout/CENTER size-label-ef)
    (.add edit-panel BorderLayout/CENTER size-text-ef)
    (.add edit-panel BorderLayout/CENTER smoking-label-ef)
    (.add edit-panel BorderLayout/CENTER smoking-text-ef)
    (.add edit-panel BorderLayout/CENTER price-night-label-ef)
    (.add edit-panel BorderLayout/CENTER price-night-text-ef)
    (.add edit-panel BorderLayout/CENTER date-label-ef)
    (.add edit-panel BorderLayout/CENTER date-text-ef)
    (.add edit-panel BorderLayout/CENTER owner-label-ef)
    (.add edit-panel BorderLayout/CENTER owner-text-ef)
    (.add edit-panel BorderLayout/SOUTH edit-button-ef)
    (.add edit-panel BorderLayout/SOUTH cancel-button-ef)
    ))
;-------------------------------------------------------------------------------
(defn book-window
  "Build the Book Register Window"
  []
  (let []
    (.add book-panel BorderLayout/CENTER id-new-owner-label)
    (.add book-panel BorderLayout/CENTER id-new-owner-text)
    (.add book-panel BorderLayout/SOUTH book-button-bf)
    (.add book-panel BorderLayout/SOUTH cancel-button-bf)
    ))
;-------------------------------------------------------------------------------
(defn edit-register
  "Sets the selected values for each text box in the edit panel."
  []
  (let [
    index  (nth (return-index
      (str (.getValueAt table (.getSelectedRow table) 0))
      (str (.getValueAt table (.getSelectedRow table) 1))
      (str (.getValueAt table (.getSelectedRow table) 2))
      (str (.getValueAt table (.getSelectedRow table) 3))
      (str (.getValueAt table (.getSelectedRow table) 4))
      (str (.getValueAt table (.getSelectedRow table) 5))
      (str (.getValueAt table (.getSelectedRow table) 6))) 0)
    ]
    (edit-register-rbf index
      (.getText hotel-name-text-ef)
      (.getText city-text-ef)
      (.getText size-text-ef)
      (.getText smoking-text-ef)
      (.getText price-night-text-ef)
      (.getText date-text-ef)
      (.getText owner-text-ef))
    (.setVisible edit-frame false)
    ))
;-------------------------------------------------------------------------------
(defn book-room
  "prueba"
  []
  (let [
    index  (nth (return-index
      (str (.getValueAt table (.getSelectedRow table) 0))
      (str (.getValueAt table (.getSelectedRow table) 1))
      (str (.getValueAt table (.getSelectedRow table) 2))
      (str (.getValueAt table (.getSelectedRow table) 3))
      (str (.getValueAt table (.getSelectedRow table) 4))
      (str (.getValueAt table (.getSelectedRow table) 5))
      (str (.getValueAt table (.getSelectedRow table) 6))) 0)
    ]
    (book-room-rbf index (.getText id-new-owner-text))
    (.setVisible book-frame false)
    ))
;-------------------------------------------------------------------------------
(defn delete-register
  "prueba manda llamar al de rbf"
  []
  (let [
    index  (nth (return-index
      (str (.getValueAt table (.getSelectedRow table) 0))
      (str (.getValueAt table (.getSelectedRow table) 1))
      (str (.getValueAt table (.getSelectedRow table) 2))
      (str (.getValueAt table (.getSelectedRow table) 3))
      (str (.getValueAt table (.getSelectedRow table) 4))
      (str (.getValueAt table (.getSelectedRow table) 5))
      (str (.getValueAt table (.getSelectedRow table) 6))) 0)
    ]
    (delete-register-rbf index)
    ))
;-------------------------------------------------------------------------------
(defn create-register
  "prueba"
  []
  (let [
    ]
    (create-register-rbf
      (.getText hotel-name-text)
      (.getText city-text)
      (.getText size-text)
      (.getText smoking-text)
      (.getText price-night-text)
      (.getText date-text)
      (.getText owner-text)
      )
    (.setVisible create-frame false)
    ))
;-------------------------------------------------------------------------------
(defn main-window
  "Displays the main window of the Zenkai Project usign the Java Swing"
  []
  (let [

    create-button-handler-mf (proxy [ActionListener][]
                              (actionPerformed [event]
                                (.setVisible create-frame true)
                                ))
    create-button-handler-cf (proxy [ActionListener][]
                              (actionPerformed [event]
                                (create-register)))
    delete-button-handler-mf (proxy [ActionListener][]
                              (actionPerformed [event]
                                (delete-register)))
    book-button-handler-mf (proxy [ActionListener][]
                          (actionPerformed [event]
                            (.setVisible book-frame true)
                            ))
    book-button-handler (proxy [ActionListener][]
                          (actionPerformed [event]
                            (book-room)))
    edit-button-handler-mf (proxy [ActionListener][]
                          (actionPerformed [event]
                            (.setVisible edit-frame true)
                                (.setText hotel-name-text-ef 
                                  (str (.getValueAt table (.getSelectedRow table) 0)))
                                (.setText city-text-ef
                                  (str (.getValueAt table (.getSelectedRow table) 1)))
                                (.setText size-text-ef 
                                  (str (.getValueAt table (.getSelectedRow table) 2)))
                                (.setText smoking-text-ef
                                  (str (.getValueAt table (.getSelectedRow table) 3)))
                                (.setText price-night-text-ef 
                                  (str (.getValueAt table (.getSelectedRow table) 4)))
                                (.setText date-text-ef
                                  (str (.getValueAt table (.getSelectedRow table) 5)))
                                (.setText owner-text-ef 
                                  (str (.getValueAt table (.getSelectedRow table) 6)))
                            ))
    edit-button-handler-ef (proxy [ActionListener][]
                          (actionPerformed [event]
                            
                            (edit-register)))
    search-button-handler-mf (proxy [ActionListener][]
                            (actionPerformed [event]
                              (cond 
                                (= (.getSelectedIndex combo-box) 0)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :name)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 1)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :location)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 2)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :size)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 3)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :smoking)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 4)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :rate)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 5)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :date)) 
                                    (into-array column-names)))
                                (= (.getSelectedIndex combo-box) 6)
                                  (.setModel table (table-model
                                    (to-array-2d (search (.getText search-text) :owner)) 
                                    (into-array column-names))))))
    ]
    (.setDefaultCloseOperation main-frame JFrame/EXIT_ON_CLOSE)
    (.setDefaultCloseOperation create-frame JFrame/HIDE_ON_CLOSE)
    (.setDefaultCloseOperation edit-frame JFrame/HIDE_ON_CLOSE)
    (.setDefaultCloseOperation book-frame JFrame/HIDE_ON_CLOSE)
    
    (.setSize main-frame (Dimension. 350 500))
    (.setSize create-frame (Dimension. 270 220))
    (.setSize edit-frame (Dimension. 270 220))
    (.setSize book-frame (Dimension. 270 120))
    
    (.setPreferredSize create-panel (Dimension. 270 220))
    (.setPreferredSize edit-panel (Dimension. 270 220))
    (.setPreferredSize book-panel (Dimension. 270 120))
    
    (.setPreferredSize button-panel (Dimension. 350 50))
    (.add button-panel BorderLayout/WEST create-button)
    (.add button-panel BorderLayout/CENTER edit-button)
    (.add button-panel BorderLayout/EAST delete-button)
    (.add button-panel BorderLayout/EAST book-button)
    
    (.setPreferredSize search-panel (Dimension. 350 70))
    (.add search-panel BorderLayout/NORTH search-text)
    (.add search-panel BorderLayout/CENTER combo-box)
    (.add search-panel BorderLayout/SOUTH search-button)
    
    (.add main-frame BorderLayout/EAST search-panel)
    (.add main-frame BorderLayout/WEST button-panel)
    
    (.addActionListener edit-button edit-button-handler-mf)
    (.addActionListener search-button search-button-handler-mf)
    (.addActionListener book-button book-button-handler-mf)
    (.addActionListener delete-button delete-button-handler-mf)
    (.addActionListener create-button create-button-handler-mf)
    
    (.addActionListener book-button-bf book-button-handler)
    (.addActionListener edit-button-ef edit-button-handler-ef)
    (.addActionListener create-button-cf create-button-handler-cf)
    
    
    (create-table)
    (create-menu-bar)
    (create-window)
    (edit-window)
    (book-window)
    
    (.add create-frame BorderLayout/EAST create-panel)
    (.add edit-frame BorderLayout/EAST edit-panel)
    (.add book-frame BorderLayout/EAST book-panel)
    
    (.pack main-frame)
    (.pack create-frame)
    (.pack edit-frame)
    (.pack book-frame)
    (.setVisible create-frame false)
    (.setVisible edit-frame false)
    (.setVisible book-frame false)
    (.setVisible main-frame true)
    ))

(defn -main
  []
  (main-window))

(generate-documentation-to-file "./GUI.html" ['GUI.Window])
